package com.jpyl.jpMedicalTreatment.view.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.lordbasewidget.viewpager.AutoScrollViewPager;
import com.forxindu.kit.box.JavaKit;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by suyongli on 2015/9/8.
 */
public class AdBannerAdapter extends PagerAdapter {

    private Context context;
    private List<String> bannerList;
    private ImageLoader imageLoader;
    private DisplayImageOptions options;
    private String userId;

    public AdBannerAdapter(Context context) {
        this.context = context;
        this.bannerList = new ArrayList<String>();
        this.imageLoader = ImageLoader.getInstance();
        this.initImageOptions();
    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(View parent, final int index) {
        ImageView image = new ImageView(context);
        image.setLayoutParams(new ViewGroup.LayoutParams(-1, -1));
        image.setMaxWidth(20);
        image.setMaxHeight(100);
        image.setScaleType(ImageView.ScaleType.CENTER_CROP);
        image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                if (!JavaKit.isListEmpty(getBannerList())) {
//                    AdVo vo = getBannerList().get(index % getBannerList().size());
//                    Intent intent = new Intent();
//                    String userId = PreferenceKit.getSharedPreference(getContext(), DietitianSettings.MEMBER_ID_KEY, "");
//                    if(userId.equals("")){
//                        intent.setClass(context, LoginActivity.class);
//                    }else{
//                        if (TicketDetailType.T_AD_INFO.equals(vo.getTablename())) {
//                            //banner跳转到web页
//                            intent.setClass(context, CommonWebActivity.class);
//                            intent.putExtra(CommonWebActivity.MATCH_ID, vo.getId());
//                        } else {
//                            //banner跳转到详细页面
//                            intent.setClass(context, MatchDetailsActivity.class);
//                            intent.putExtra(MatchDetailsActivity.T_NAME, vo.getTablename());
//                            intent.putExtra(MatchDetailsActivity.MATCH_ID, vo.getId());
//                        }
//                    }
//                    context.startActivity(intent);
//                    if(userId.equals("")){
//                        HomeActivity.activity.finish();
//                    }
//                }
            }
        });
        if (!JavaKit.isListEmpty(getBannerList())) {
            if (getBannerList().size() > 0) {
//                imageLoader.displayImage("" + getBannerList().get(index % getBannerList().size()).getPath(), image, options, new SimpleImageLoadingListener());
            }
        }
        ((AutoScrollViewPager) parent).addView(image);
        return image;
    }

    public void destroyItem(View parent, int index, Object currentItem) {
        ((AutoScrollViewPager) parent).removeView((ImageView) currentItem);
    }

    public List<String> getBannerList() {
        return bannerList;
    }

    public void setBannerList(List<String> bannerList) {
        this.bannerList = bannerList;
    }

    private void initImageOptions() {
        options = new DisplayImageOptions.Builder()
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .imageScaleType(ImageScaleType.EXACTLY_STRETCHED)
                .bitmapConfig(Bitmap.Config.ARGB_8888)
                .displayer(new SimpleBitmapDisplayer()).handler(new Handler()).build();
    }

    public Context getContext() {
        return context;
    }
}
